﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DeployableProjectOperations.cs" company="Elilink">
//   Defor
// </copyright>
// <summary>
//   Helper class for common operations on Deployable projects
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DataAccessLayer.StaticTools
{
    using System.Linq;

    using BusinessLayer.Enums;
    using BusinessLayer.Objects;
    using BusinessLayer.Results;

    using DataAccessLayer.Repositories.Static;

    /// <summary>
    ///     Helper class for common operations on Deployable projects
    /// </summary>
    public class DeployableProjectOperations
    {
        #region Public Methods and Operators

        /// <summary>
        /// Adds the deployable project.
        /// </summary>
        /// <param name="project">
        /// The project.
        /// </param>
        /// <returns>
        /// The <see cref="OperationResult"/>.
        /// </returns>
        public static OperationResult AddDeployableProject(DeployableProject project)
        {
            if (string.IsNullOrEmpty(project.TargetSystemName))
            {
                return new OperationResult(
                    ErrorType.TargetSystemNameNotSpecified, 
                    "Can't add project without reference to target system.");
            }

            if (string.IsNullOrEmpty(project.ProjectName))
            {
                return new OperationResult(ErrorType.ProjectNameNotSpecified, "Can't add project without name.");
            }

            if (StaticDatabase.TargetSystems.FirstOrDefault(t => t.TargetSystemName == project.TargetSystemName) == null)
            {
                var message = string.Format(
                    "Can't find target '{1}' for updating project '{0}'.",
                    project.ProjectName,
                    project.TargetSystemName);
                return new OperationResult(
                    ErrorType.ObjectNotFound, message);
            }

            if (
                StaticDatabase.DeployableProjects.FirstOrDefault(
                    p => p.ProjectName == project.ProjectName && p.TargetSystemName == project.TargetSystemName) != null)
            {
                var message = string.Format(
                    "Project '{0}' already exists in target '{1}'.",
                    project.ProjectName,
                    project.TargetSystemName);
                return new OperationResult(
                    ErrorType.AlreadyExists, message);
            }

            StaticDatabase.DeployableProjects.Add(project);
            return new OperationResult(true);
        }

        #endregion
    }
}